package com.iemes.controller.jurisdiction;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ButtomFormMap;
import com.iemes.entity.PositionFormMap;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.ResRoleFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.PositionMapper;
import com.iemes.mapper.ResourcesMapper;
import com.iemes.mapper.RoleResourcesMapper;
import com.iemes.util.Common;
import com.iemes.util.TreeObject;
import com.iemes.util.TreeUtil;


@RequestMapping("/menulist/")
@Controller
public class JurisdictionController extends BaseController {
	@Inject
	private ResourcesMapper resourcesMapper;
	@Inject
	private PositionMapper positionMapper;
	@Inject
	private RoleResourcesMapper roleResourcesMapper;
	/**
	 * @param model
	 *            存放返回界面的model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("treelists")
	public ResFormMap findByPage(Model model  ,HttpServletRequest request) {
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		if (!userFormMap.getStr("site").equals("*")) {
			return null;
		}
		// 获取此账号的部门信息
		PositionFormMap positionFormMap = positionMapper.findbyFrist("id",
				userFormMap.get("position_id").toString(),
				PositionFormMap.class);
		ResFormMap resFormMap = getFormMap(ResFormMap.class);
		resFormMap.put("type", positionFormMap.get("departid"));
		String order = " order by level asc";
		resFormMap.put("$orderby", order);
		List<ResFormMap> mps = resourcesMapper.findByNames(resFormMap);
		List<TreeObject> list = new ArrayList<TreeObject>();
		for (ResFormMap map : mps) {
			TreeObject ts = new TreeObject();
			Common.flushObject(ts, map);
			list.add(ts);
		}
		TreeUtil treeUtil = new TreeUtil();
		List<TreeObject> ns = treeUtil.getChildTreeObjects(list, 0);
		resFormMap = new ResFormMap();
		resFormMap.put("treelists", ns);
		return resFormMap;
	}

	@ResponseBody
	@RequestMapping("reslists")
	public List<TreeObject> reslists(Model model,HttpServletRequest request) throws Exception {
		
		ResFormMap resFormMap = getFormMap(ResFormMap.class);
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		if (!userFormMap.getStr("site").equals("*")) {
			return null;
		}
		// 获取此账号的部门信息
		PositionFormMap positionFormMap = positionMapper.findbyFrist("id",
				userFormMap.get("position_id").toString(),
				PositionFormMap.class);
		// 目前暂定 老板为最大boss拥有所有权限
		resFormMap.put("type", positionFormMap.get("departid"));
		List<ResFormMap> mps = resourcesMapper.findByWhere(resFormMap);
		List<TreeObject> list = new ArrayList<TreeObject>();
		for (ResFormMap map : mps) {
			TreeObject ts = new TreeObject();
			Common.flushObject(ts, map);
			list.add(ts);
		}
		TreeUtil treeUtil = new TreeUtil();
		List<TreeObject> ns = treeUtil.getChildTreeObjects(list, 0, "　");
		return ns;
	}

	/**
	 * @param model
	 *            存放返回界面的model
	 * @return
	 */
	@RequestMapping("list")
	public String list(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/system/resources/list";
	}

	/**
	 * 跳转到修改界面
	 * 
	 * @param model
	 * @param resourcesId
	 *            修改菜单信息ID
	 * @return
	 */
	@RequestMapping("editUI")
	public String editUI(Model model) {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			model.addAttribute("resources", resourcesMapper.findbyFrist("id", id, ResFormMap.class));
		}
		return Common.BACKGROUND_PATH + "/system/resources/edit";
	}

	/**
	 * 跳转到新增界面
	 * 
	 * @return
	 */
	@RequestMapping("addUI")
	public String addUI(Model model) {
		return Common.BACKGROUND_PATH + "/system/resources/add";
	}

	/**
	 * 权限分配页面
	 * 
	 * @author lanyuan Email：mmm333zzz520@163.com date：2014-4-14
	 * @param model
	 * @return
	 */
	@RequestMapping("permissions")
	public String permissions(Model model,HttpServletRequest request) {
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		if (!userFormMap.getStr("site").equals("*")) {
			return null;
		}
		PositionFormMap positionFormMap = positionMapper.findbyFrist("id",
				userFormMap.get("position_id").toString(),
				PositionFormMap.class);
		ResFormMap resFormMap = getFormMap(ResFormMap.class);
		resFormMap.put("type",  positionFormMap.get("departid").toString());
		List<ResFormMap> mps = resourcesMapper.findByNames(resFormMap);
		List<TreeObject> list = new ArrayList<TreeObject>();
		for (ResFormMap map : mps) {
			TreeObject ts = new TreeObject();
			Common.flushObject(ts, map);
			list.add(ts);
		}
		TreeUtil treeUtil = new TreeUtil();
		List<TreeObject> ns = treeUtil.getChildTreeObjects(list, 0);
		model.addAttribute("permissions", ns);
		return Common.BACKGROUND_PATH + "/system/resources/permissions";
	}

	/**
	 * 添加菜单
	 * 
	 * @param resources
	 * @return Map
	 * @throws Exception
	 *//*
	@RequestMapping("addEntity")
	@ResponseBody
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="系统管理",methods="资源管理-新增资源")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		ResFormMap resFormMap = getFormMap(ResFormMap.class);
		if("2".equals(resFormMap.get("type"))){
			resFormMap.put("description", Common.htmltoString(resFormMap.get("description")+""));
		}
		Object o = resFormMap.get("ishide");
		if(null==o){
			resFormMap.set("ishide", "0");
		}
		
		resourcesMapper.addEntity(resFormMap);
		return "success";
	}*/

	/**
	 * 更新菜单
	 * 
	 * @param model
	 * @param Map
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="系统管理",methods="资源管理-修改资源")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(Model model) throws Exception {
		ResFormMap resFormMap = getFormMap(ResFormMap.class);
		if("2".equals(resFormMap.get("type"))){
			resFormMap.put("description", Common.htmltoString(resFormMap.get("description")+""));
		}
		Object o = resFormMap.get("ishide");
		if(null==o){
			resFormMap.set("ishide", "0");
		}
		resourcesMapper.editEntity(resFormMap);
		return "success";
	}

//	/**
//	 * 根据ID删除菜单
//	 * 
//	 * @param model
//	 * @param ids
//	 * @return
//	 * @throws Exception
//	 */
//	@ResponseBody
//	@RequestMapping("deleteEntity")
//	@SystemLog(module="系统管理",methods="资源管理-删除资源")//凡需要处理业务逻辑的.都需要记录操作日志
//	public String deleteEntity(Model model) throws Exception {
//		String[] ids = getParaValues("ids");
//		for (String id : ids) {
//			resourcesMapper.deleteByAttribute("id", id, ResFormMap.class);
//		};
//		return "success";
//	}
//
//	@RequestMapping("sortUpdate")
//	@ResponseBody
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
//	public String sortUpdate(Params params) throws Exception {
//		List<String> ids = params.getId();
//		List<String> es = params.getRowId();
//		List<ResFormMap> maps = new ArrayList<ResFormMap>();
//		for (int i = 0; i < ids.size(); i++) {
//			ResFormMap map = new ResFormMap();
//			map.put("id", ids.get(i));
//			map.put("level", es.get(i));
//			maps.add(map);
//		}
//		resourcesMapper.updateSortOrder(maps);
//		return "success";
//	}

	@ResponseBody
	@RequestMapping("findRes")
	public List<ResFormMap> findUserRes() {
		ResFormMap resFormMap = getFormMap(ResFormMap.class);
		List<ResFormMap> rs = resourcesMapper.findRes(resFormMap);
		return rs;
	}
	@ResponseBody
	@RequestMapping("addUserRes")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="系统管理",methods="用户管理/组管理-修改权限")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addUserRes() {
////		String userId = "";
////		String u = getPara("userId");
////		String g = getPara("roleId");
////		if (null != u && !Common.isEmpty(u.toString())) {
////			userId = u.toString();
////		} else if (null != g && !Common.isEmpty(g.toString())) {
////			List<UserGroupsFormMap> gs = resourcesMapper.findByAttribute("roleId", g.toString(), UserGroupsFormMap.class);
////			for (UserGroupsFormMap ug : gs) {
////				userId += ug.get("userId") + ",";
////			}
////		}
////		userId = Common.trimComma(userId);
////		if(Common.isEmpty(userId)){
////			return "分配失败,该角色下没有用户!";
////		}
////		String[] users = userId.split(",");
////		for (String uid : users) {
////			resourcesMapper.deleteByAttribute("userId", uid, ResUserFormMap.class);
////			String[] s = getParaValues("resId[]");
////			List<ResUserFormMap> resUserFormMaps = new ArrayList<ResUserFormMap>();
////			for (String rid : s) {
////				ResUserFormMap resUserFormMap = new ResUserFormMap();
////				resUserFormMap.put("resId", rid);
////				resUserFormMap.put("userId", uid);
////				resUserFormMaps.add(resUserFormMap);
////			
////			}
////			resourcesMapper.batchSave(resUserFormMaps);
////		}
////		return "success";
//		
		/*
		 * Liuyang更改用户授权为角色授权
		 */
		String roleId = getPara("roleId");
		try {
			resourcesMapper.deleteByAttribute("roleId", roleId, ResRoleFormMap.class);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return "false";
		}
		String[] s = getParaValues("resId[]");
		for (String rid : s) {
			ResRoleFormMap resRoleFormMap = new ResRoleFormMap();
			resRoleFormMap.put("menuid", rid);
			resRoleFormMap.put("roleid", roleId);
			try {
				roleResourcesMapper.addEntity(resRoleFormMap);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "fasle";
			}
		}
		
		return "success";
	}

	@ResponseBody
	@RequestMapping("findByButtom")
	public List<ButtomFormMap> findByButtom(){
		return resourcesMapper.findByWhere(new ButtomFormMap());
	}
	
//	/**
//	 * 验证菜单是否存在
//	 * 
//	 * @param name
//	 * @return
//	 */
//	@RequestMapping("isExist")
//	@ResponseBody
//	public boolean isExist(String name,String resKey) {
//		ResFormMap resFormMap = getFormMap(ResFormMap.class);
//		List<ResFormMap> r = resourcesMapper.findByNames(resFormMap);
//		if (r.size()==0) {
//			return true;
//		} else {
//			return false;
//		}
//	}
}
